package com.example.boxueguapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.boxueguapplication.model.XiTi;
import com.thoughtworks.xstream.XStream;

import java.io.InputStream;
import java.util.List;

public class XiTiActivity extends AppCompatActivity {

    private List<XiTi.XiTiItem> xitis;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xi_ti);
        findView();
        Intent intent = getIntent();
        mTitleBar.setText(intent.getStringExtra("title"));
        mBack.setOnClickListener(view -> finish());
        Integer raw = null;
        switch ((int) intent.getLongExtra("id", 0L)) {
            case 1:
                raw = R.raw.chapter1;
                break;
            case 2:
                raw = R.raw.chapter2;
                break;
            case 3:
                raw = R.raw.chapter3;
                break;
            case 4:
                raw = R.raw.chapter4;
                break;
            case 5:
                raw = R.raw.chapter5;
                break;
            case 6:
                raw = R.raw.chapter6;
                break;
            case 7:
                raw = R.raw.chapter7;
                break;
            case 8:
                raw = R.raw.chapter8;
                break;
            case 9:
                raw = R.raw.chapter9;
                break;
            case 10:
                raw = R.raw.chapter10;
                break;
            default:
                break;
        }
        InputStream in = getResources().openRawResource(raw);
        XStream xstream = new XStream();
        xstream.processAnnotations(XiTi.class);
        XiTi xiTi = (XiTi) xstream.fromXML(in);
        xitis = xiTi.xitis;
        mXitiList.setLayoutManager(new LinearLayoutManager(this));
        XiTiAdapter xiTiAdapter = new XiTiAdapter(R.layout.xiti_item, xitis);
        mXitiList.setAdapter(xiTiAdapter);
    }

    private RelativeLayout mAppBar;
    private ImageView mBack;
    private TextView mTitleBar;
    private RecyclerView mXitiList;

    private void findView() {
        mAppBar = findViewById(R.id.app_bar);
        mBack = findViewById(R.id.back);
        mTitleBar = findViewById(R.id.title_bar);
        mXitiList = findViewById(R.id.xiti_list);
    }

    public class XiTiAdapter extends BaseQuickAdapter<XiTi.XiTiItem, BaseViewHolder> {

        public XiTiAdapter(int layoutResId, List<XiTi.XiTiItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, XiTi.XiTiItem xiTiItem) {
            holder.setText(R.id.xiti_title, xiTiItem.subject);
            holder.setText(R.id.item_a_context, xiTiItem.a);
            holder.setText(R.id.item_b_context, xiTiItem.b);
            holder.setText(R.id.item_c_context, xiTiItem.c);
            holder.setText(R.id.item_d_context, xiTiItem.d);
            ImageView A = holder.findView(R.id.item_a);
            ImageView B = holder.findView(R.id.item_b);
            ImageView C = holder.findView(R.id.item_c);
            ImageView D = holder.findView(R.id.item_d);
            View.OnClickListener onclick = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ImageView item = (ImageView) view;
                    int answer = 0;
                    switch (item.getId()) {
                        case R.id.item_a:
                            answer = 1;
                            break;
                        case R.id.item_b:
                            answer = 2;
                            break;
                        case R.id.item_c:
                            answer = 3;
                            break;
                        case R.id.item_d:
                            answer = 4;
                        default:
                            break;
                    }
                    if (answer != xiTiItem.answer) {
                        item.setImageResource(R.drawable.exercises_error_icon);
                        int answerId = 0;
                        switch (xiTiItem.answer) {
                            case 1:
                                answerId = R.id.item_a;
                                break;
                            case 2:
                                answerId = R.id.item_b;
                                break;
                            case 3:
                                answerId = R.id.item_c;
                                break;
                            case 4:
                                answerId = R.id.item_d;
                            default:
                                break;
                        }
                        ((ImageView) holder.findView(answerId)).setImageResource(R.drawable.exercises_right_icon);
                    } else {
                        item.setImageResource(R.drawable.exercises_right_icon);
                    }
                    holder.setEnabled(R.id.item_a,false);
                    holder.setEnabled(R.id.item_b,false);
                    holder.setEnabled(R.id.item_c,false);
                    holder.setEnabled(R.id.item_d,false);
                }
            };
            A.setOnClickListener(onclick);
            B.setOnClickListener(onclick);
            C.setOnClickListener(onclick);
            D.setOnClickListener(onclick);
        }
    }
}